(function() {
  var Task, TaskController, TomatoBreak;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  TomatoBreak = this.TomatoBreak;
  Task = TomatoBreak.Task;
  TaskController = (function() {
    __extends(TaskController, TomatoBreak.FormController);
    function TaskController() {
      this.change = __bind(this.change, this);      TaskController.__super__.constructor.apply(this, arguments);
      Task.bind('error', __bind(function(task, errors) {
        return this.showErrors(errors);
      }, this));
      Task.bind('change', __bind(function() {
        return this.navigate('!/tasks', true);
      }, this));
    }
    TaskController.prototype.change = function(id) {
      var form, load;
      this.id = id;
      form = this.form;
      load = function() {
        return form.deserializeForm(Task.find(id));
      };
      if (Task.count()) {
        return load();
      } else {
        return Task.fetch(function() {
          return load();
        });
      }
    };
    TaskController.prototype.submit = function(event) {
      var attributes, dueAt, task, _ref;
      event.preventDefault();
      attributes = this.form.serializeForm();
      if (attributes.dueAt) {
        dueAt = (_ref = Date.parse(attributes.dueAt)) != null ? _ref.toString(TomatoBreak.DATE_FORMAT) : void 0;
        if (dueAt != null) {
          attributes.dueAt = dueAt;
        }
      }
      if (this.id != null) {
        task = Task.find(this.id);
        task.load(attributes);
      } else {
        task = new Task(attributes);
      }
      return task.save();
    };
    return TaskController;
  })();
  TomatoBreak.TaskController = TaskController;
}).call(this);
